package com.yellowpage.ndc.action;
  
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.ndc.data.Listing;
import com.yellowpage.ndc.service.ListingService;

public class ListingAction extends BaseAction {

    public String RESULT_QUERY = "query";
    public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	
	private Configuration configuration;
	
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}
	
	private List<Listing> listingList = new ArrayList<Listing>();
	private List<Map> resultList = new ArrayList<Map>();
	
	private Listing listing;
	private ListingService listingService;
	
	private String selectedId;	
	private String[] selectedIds;	
	private String message;
	
		private String query_listId;
		private String query_custId;
		private String query_addrId;
		private String query_subname1;
		private String query_subname2;
		private String query_subname3;
		private String query_subname4;
		private String query_tel;
		private String query_telExt;
		private String query_faxFlag;
		private String query_sb;
		private String query_listOrder;
		private String query_note;
		private String query_listStatus;
		private String query_modifyTime;
		private String query_modifyEmpId;
		private String query_findwayId;
		private String query_source;
		
	public void prepare() {
		super.prepare();
	
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.listing = listingService.getListing(Long.parseLong(selectedId));
		}
	}
	
	public String queryByPage() {		
		initPageRender();
		
		Map queryMap = new HashMap();	
			    queryMap.put("listId", query_listId);
	    	    queryMap.put("custId", query_custId);
	    	    queryMap.put("addrId", query_addrId);
	    	    queryMap.put("subname1", query_subname1);
	    	    queryMap.put("subname2", query_subname2);
	    	    queryMap.put("subname3", query_subname3);
	    	    queryMap.put("subname4", query_subname4);
	    	    queryMap.put("tel", query_tel);
	    	    queryMap.put("telExt", query_telExt);
	    	    queryMap.put("faxFlag", query_faxFlag);
	    	    queryMap.put("sb", query_sb);
	    	    queryMap.put("listOrder", query_listOrder);
	    	    queryMap.put("note", query_note);
	    	    queryMap.put("listStatus", query_listStatus);
	    	    queryMap.put("modifyTime", query_modifyTime);
	    	    queryMap.put("modifyEmpId", query_modifyEmpId);
	    	    queryMap.put("findwayId", query_findwayId);
	    	    queryMap.put("source", query_source);
	    	    	
		resultList = listingService.queryListingByPage(queryMap, pageRender);
		
		setPageRenderToRequest();

		return RESULT_QUERY;
	}
	
	public String toAdd() {
		return RESULT_ADD;
	}
	
	public String save() {
	    
		listingService.saveListing(listing);		
		
		message = "新增成功!";
		return RESULT_ADD;
	}
	
	public void validateSave() {
		if (listing.getTel().length() == 0) {
			this.addActionError("** 电话不能为空 !");
		}

		this.setValidatorResult("/ndc/ListingAdd.jsp");	
	}
	
	public String toEdit() {
	    Long id = new Long(selectedId);
	    
	    listing = listingService.getListing(id);
	
		return RESULT_EDIT;
	}
	
	public String update() {
	
	    listingService.updateListing(listing);	

		message = "修改成功!";
	    
		return RESULT_EDIT;
	}
	
	public void validateUpdate() {
		if (listing.getTel().length() == 0) {
			this.addActionError("** Tel 不能为空 !");
		}
		this.setValidatorResult("/ndc/ListingUpdate.jsp");
	}
	
	public String delete() {	    
	    List ids = new ArrayList();
	    
		for(String id : selectedIds){
			ids.add(Long.valueOf(id));
		}
		
		try {
			this.listingService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲突删除失败 !");
		}
		
		
		return queryByPage();
	}	
	
	

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	 public void setListing(Listing listing) {
		this.listing = listing;
	}
	
	public Listing getListing() {
		return listing;
	}
	 
	 
	 public void setListingService(ListingService listingService) {
		this.listingService = listingService;
	}
	
	public List<Listing> getListingList() {
		return listingList;
	}

	public void setListingList(List<Listing> listingList) {
		this.listingList = listingList;
	}
	
	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}
	
	    public String getQuery_listId() {
        return this.query_listId;
    }
    
    public void setQuery_listId(String listId){
        this.query_listId = listId;
    }    

        public String getQuery_custId() {
        return this.query_custId;
    }
    
    public void setQuery_custId(String custId){
        this.query_custId = custId;
    }    

        public String getQuery_addrId() {
        return this.query_addrId;
    }
    
    public void setQuery_addrId(String addrId){
        this.query_addrId = addrId;
    }    

        public String getQuery_subname1() {
        return this.query_subname1;
    }
    
    public void setQuery_subname1(String subname1){
        this.query_subname1 = subname1;
    }    

        public String getQuery_subname2() {
        return this.query_subname2;
    }
    
    public void setQuery_subname2(String subname2){
        this.query_subname2 = subname2;
    }    

        public String getQuery_subname3() {
        return this.query_subname3;
    }
    
    public void setQuery_subname3(String subname3){
        this.query_subname3 = subname3;
    }    

        public String getQuery_subname4() {
        return this.query_subname4;
    }
    
    public void setQuery_subname4(String subname4){
        this.query_subname4 = subname4;
    }    

        public String getQuery_tel() {
        return this.query_tel;
    }
    
    public void setQuery_tel(String tel){
        this.query_tel = tel;
    }    

        public String getQuery_telExt() {
        return this.query_telExt;
    }
    
    public void setQuery_telExt(String telExt){
        this.query_telExt = telExt;
    }    

        public String getQuery_faxFlag() {
        return this.query_faxFlag;
    }
    
    public void setQuery_faxFlag(String faxFlag){
        this.query_faxFlag = faxFlag;
    }    

        public String getQuery_sb() {
        return this.query_sb;
    }
    
    public void setQuery_sb(String sb){
        this.query_sb = sb;
    }    

        public String getQuery_listOrder() {
        return this.query_listOrder;
    }
    
    public void setQuery_listOrder(String listOrder){
        this.query_listOrder = listOrder;
    }    

        public String getQuery_note() {
        return this.query_note;
    }
    
    public void setQuery_note(String note){
        this.query_note = note;
    }    

        public String getQuery_listStatus() {
        return this.query_listStatus;
    }
    
    public void setQuery_listStatus(String listStatus){
        this.query_listStatus = listStatus;
    }    

        public String getQuery_modifyTime() {
        return this.query_modifyTime;
    }
    
    public void setQuery_modifyTime(String modifyTime){
        this.query_modifyTime = modifyTime;
    }    

        public String getQuery_modifyEmpId() {
        return this.query_modifyEmpId;
    }
    
    public void setQuery_modifyEmpId(String modifyEmpId){
        this.query_modifyEmpId = modifyEmpId;
    }    

        public String getQuery_findwayId() {
        return this.query_findwayId;
    }
    
    public void setQuery_findwayId(String findwayId){
        this.query_findwayId = findwayId;
    }    

        public String getQuery_source() {
        return this.query_source;
    }
    
    public void setQuery_source(String source){
        this.query_source = source;
    }    

    
	
}
